package se.perp.c4.ui;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import se.perp.c4.model.Actor.Side;
import se.perp.c4.model.Card;
import se.perp.c4.model.DarkDeck;
import se.perp.c4.model.LightDeck;
import static se.perp.c4.model.Card.ECard.*;

public class Images {


    public enum Which {

        APPICON, LIGHTCARD_CENTERIMAGE, DARKCARD_CENTERIMAGE,
        SHADOWFAX, GANDALF_WHITE, RECALL_TO_MORDOR, PALANTIR,
        SHADOWFAX_SMALL, GANDALF_WHITE_SMALL, RECALL_TO_MORDOR_SMALL, PALANTIR_SMALL,
        CONFRONTATION, GLOBE,  TABLE, MAGIC_VS_EYE
    };
    private static Map<Which, BufferedImage> images = new HashMap<Which, BufferedImage>();
    private static Map<Which, String> paths = new HashMap<Which, String>();
    private static Map<Card.ECard, BufferedImage> darkCardImages = new HashMap<Card.ECard, BufferedImage>();
    private static Map<Card.ECard, BufferedImage> lightCardImages = new HashMap<Card.ECard, BufferedImage>();
    public static final Icon APPICON;
    public static final Icon CONFRONTATION;
    public static final Icon GLOBE;
        public static final Icon MAGIC_VS_EYE;

    static {
        paths.put(Which.TABLE, "/images/table2.jpg");
        paths.put(Which.APPICON, "/images/appicon.png");
        paths.put(Which.LIGHTCARD_CENTERIMAGE, "/images/gandalf_oval.png");
        paths.put(Which.DARKCARD_CENTERIMAGE, "/images/eye_oval.png");

        paths.put(Which.SHADOWFAX, "/images/shadowfax.jpg");
        paths.put(Which.GANDALF_WHITE, "/images/gandalf_white.jpg");
        paths.put(Which.RECALL_TO_MORDOR, "/images/mordor.jpg");
        paths.put(Which.PALANTIR, "/images/palantir.jpg");
        paths.put(Which.SHADOWFAX_SMALL, "/images/shadowfax_small.jpg");
        paths.put(Which.GANDALF_WHITE_SMALL, "/images/gandalf_white_small.jpg");
        paths.put(Which.RECALL_TO_MORDOR_SMALL, "/images/mordor_small.jpg");
        paths.put(Which.PALANTIR_SMALL, "/images/palantir_small.jpg");

        paths.put(Which.CONFRONTATION, "/images/confrontation.png");
        paths.put(Which.GLOBE, "/images/globe.png");
        paths.put(Which.MAGIC_VS_EYE, "/images/magicvseye.png");

        APPICON = new ImageIcon(get(Which.APPICON));
        CONFRONTATION = new ImageIcon(get(Which.CONFRONTATION));
        GLOBE = new ImageIcon(get(Which.GLOBE));
        MAGIC_VS_EYE = new ImageIcon(get(Which.MAGIC_VS_EYE));

        LightDeck ld = new LightDeck();
        DarkDeck dd = new DarkDeck();
        for (Card c : ld.getDeck()) {
            lightCardImages.put(c.type, createCardImage(c));
        }
        for (Card c: dd.getDeck()) {
            darkCardImages.put(c.type, createCardImage(c));
        }
    }

    public static BufferedImage get(Which which) {

        BufferedImage img = null;
        if (!images.keySet().contains(which)) {
            String path = paths.get(which);
            try {
                img = ImageIO.read(Images.class.getResourceAsStream(path));
                images.put(which, img);
            } catch (IOException ex) {
                Logger.getLogger(Images.class.getName()).log(Level.SEVERE, "Could not load image " + path, ex);
            }
        } else {
            img = images.get(which);
        }
        return img;
    }

    public static BufferedImage getCardImage(Card card) {
        return (card.side == Side.DARK ? darkCardImages.get(card.type) : lightCardImages.get(card.type));
    }


    private static BufferedImage createCardImage(Card card) {

        final int WIDTH = 120;
        final int HEIGHT = 160;
        BufferedImage img = new BufferedImage(WIDTH + 1, HEIGHT + 1, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = (Graphics2D) img.getGraphics();

        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g.setColor(Color.WHITE);
        g.fillRoundRect(0, 0, WIDTH, HEIGHT, 15, 15);
        g.setColor(Color.BLACK);
        g.drawRoundRect(0, 0, WIDTH, HEIGHT, 15, 15);

        Color cardBG;
        if (card.side == Side.LIGHT) {
            cardBG = ColorSet.LIGHTCARD_BG;
        } else {
            cardBG = ColorSet.DARKCARD_BG;
        }

        g.setColor(cardBG);
        g.fillRoundRect(10, 10, WIDTH - 20, HEIGHT - 20, 15, 15);

        Color cardText;
        if (card.side == Side.LIGHT) {
            cardText = ColorSet.LIGHTCARD_TEXT;
        }else {
            cardText = ColorSet.DARKCARD_TEXT;
        }

        g.setColor(cardText);
        int attackValue = Card.attackValue(card.type);
        if (attackValue != 0) {
            g.setFont(FontSet.CARD_ATK_VALUE);
            g.drawString(Integer.toString(Card.attackValue(card.type)),
                    12, 42);
        } else {
            String s = null;
            switch (card.type) {
                case MAGIC:
                    s = "Magic";
                    break;
                case ELVEN_CLOAK:
                    s = "Elven Cloak";
                    break;
                case EYE_OF_SAURON:
                    s = "Eye of Sauron";
                    break;
                case NOBLE_SACRIFICE:
                    s = "Noble Sacrifice";
                    break;
                case RETREAT_BACK:
                case RETREAT_SIDEWAYS:
                    s = "Retreat";
                    break;
                default:
                    Logger.getLogger(Images.class.getName()).log(
                            Level.SEVERE, "Illegal card type: " + card.type);
                    break;

            }
            g.setFont(FontSet.CARD_CAPTION);
            int strW = g.getFontMetrics().stringWidth(s);
            int x = (WIDTH - strW) / 2;
            g.drawString(s, x, 30);
        }

        BufferedImage centerImage;
        if (card.side == Side.DARK) {
            centerImage = Images.get(Which.DARKCARD_CENTERIMAGE);
        } else {
            centerImage = Images.get(Which.LIGHTCARD_CENTERIMAGE);
        }
        int ovalX = WIDTH / 2 - centerImage.getWidth() / 2;
        int ovalY = HEIGHT / 2 - centerImage.getHeight() / 2;

        g.drawImage(centerImage, ovalX, ovalY, null);

        return img;
    }
}
